package com.ruoyi.quartz.task;

import cn.hutool.core.lang.Console;
import com.google.common.collect.Lists;
import com.google.protobuf.ServiceException;
import com.ruoyi.adrss.Http;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.carservicestatics.domain.TCarServiceCollect;
import com.ruoyi.management.carservicestatics.domain.TCarServiceStatics;
import com.ruoyi.management.carservicestatics.service.ITCarServiceCollectService;
import com.ruoyi.management.carservicestatics.service.ITCarServiceStaticsService;
import com.ruoyi.passenger.order.domain.TOrder;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.metrics.StartupStep;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 同步车辆营运信息到订单
 * 
 * @author lss
 */
@Component("carServiceOrderTask")
public class CarServiceOrderTask
{

    @Autowired
    private ITOrderService tOrderService;


    @Autowired
    private ITCarServiceCollectService tCarServiceCollectService;
    @Autowired
    private ITCarServiceStaticsService tCarServiceStaticsService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 同步车辆营运信息到订单
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncOrder(){
        //获取未同步行程信息的订单
        List<TOrder> orderLists = Lists.newArrayList();
        List<TCarServiceStatics> staticsList = Lists.newArrayList();
        List<String> collectIdList = Lists.newArrayList();
        List<TOrder> orderList = tOrderService.getOrderListWithNoSync();
        for (TOrder order : orderList)
        {
            //获取本次订单对应的行程信息
            TCarServiceCollect collect = tCarServiceCollectService.getCarServiceCollectByOrder(order);
            if(collect == null){
                continue;
            }
            //上车经纬度拼接
            String startLocation = collect.getStartLocation();
            String endLocation = collect.getEndLocation();
            if(StringUtils.isEmpty(startLocation) || StringUtils.isEmpty(endLocation)){
                continue;
            }
            String[] starts = startLocation.split(",");
            StringBuilder startBuilder = new StringBuilder();
            startBuilder.append(starts[1]);
            startBuilder.append(",");
            startBuilder.append(starts[0]);
            startLocation = startBuilder.toString();
            //下车经纬度拼接
            String[] ends = endLocation.split(",");
            StringBuilder endBuilder = new StringBuilder();
            endBuilder.append(ends[1]);
            endBuilder.append(",");
            endBuilder.append(ends[0]);
            endLocation = endBuilder.toString();
            //获取调用百度地图接口密钥ak
            String ak = configService.selectConfigByKey("ak");
            if(StringUtils.isEmpty(ak)){
                continue;
            }
            String startSite = "";
            String endSite = "";
            //调用百度地图接口
            StringBuilder startUrl = new StringBuilder();
            startUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            startUrl.append(ak);
            startUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            startUrl.append(startLocation);
            startSite = Http.doGet(startUrl.toString());
            if(startSite.equals("")){
                startSite = Http.doGet(startUrl.toString());
            }
            StringBuilder endUrl = new StringBuilder();
            endUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            endUrl.append(ak);
            endUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            endUrl.append(endLocation);
            endSite = Http.doGet(endUrl.toString());
            if(endSite.equals("")){
                endSite = Http.doGet(endUrl.toString());
            }
            //更新订单信息
            TOrder tOrder = new TOrder();
            tOrder.setId(order.getId());
            tOrder.setStartSite(startSite);
            tOrder.setEndSite(endSite);
            tOrder.setStartTime(collect.getStartTime());
            tOrder.setEndTime(collect.getEndTime());
            tOrder.setStartCoordinate(collect.getStartLocation());
            tOrder.setEndCoordinate(collect.getEndLocation());
            tOrder.setMileage(collect.getMileage());
            tOrder.setIsSync(1);
            tOrder.setRealAmount(collect.getAmount());
            orderLists.add(tOrder);

                //更新驾驶员建行卡收款营运信息
            TCarServiceStatics statics = new TCarServiceStatics();
            statics.setCarNo(order.getCarNo());
            statics.setEmployeeId(order.getEmployeeId());
            statics.setEmployeeName(order.getEmployeeName());
            statics.setOperationTime(order.getOrderDate());
            statics.setOrderId(order.getId());
            statics.setAmount(order.getAmount());
            statics.setYear(DateUtils.getYear(order.getOrderDate()));
            statics.setMonth(DateUtils.getMonth(order.getOrderDate()));
            statics.setDay(DateUtils.getDD(order.getOrderDate()));
            statics.setCompanyId(order.getCompanyId());
            staticsList.add(statics);

                //更新行程信息为已同步
            collectIdList.add(collect.getId());
        }
        if(!CollectionUtils.isEmpty(orderLists)){
            tOrderService.updateOrderList(orderLists);
        }
        if(!CollectionUtils.isEmpty(staticsList)){
            tCarServiceStaticsService.saveCarServiceStaticsList(staticsList);
        }
        if(!CollectionUtils.isEmpty(collectIdList)){
            tCarServiceCollectService.updateTCarServiceCollectList(collectIdList);
        }
    }
}
